﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.Utils.Win;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.门店下单.DTO;
using DXApplicationTangche.UC.销售记录.model;
using DXApplicationTangche.UC.销售记录.service;
using DevExpress.XtraGrid.Demos.util;
using static DXApplicationTangche.UC.门店下单.form.Frm订单一览;
using DXApplicationTangche.UC.门店下单;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using System.IO;
using mendian;
using DXApplicationTangche.UC.订单一览新.销售确认单;
using DXApplicationTangche.UC.Luna;

namespace DXApplicationTangche.UC.销售记录
{
    public partial class Form销售记录 : DevExpress.XtraEditors.XtraForm
    {
        private String shopId;
        private String shopName;
        private String empId;
        private String empName;
        private String wayId;
        private String wayName;
        private String customer;
        private OrderModel orderModel;
        //private List<OrderDto> orderDtos = new List<OrderDto>();
        private List<OrderCommitDTO返回> OrderCommitDtos = new List<OrderCommitDTO返回>();
        private List<OrderCommitFacade> OrderCommitFacade = new List<OrderCommitFacade>();
        private Enum订单类别 Enum订单类别;
        private List<String> quoteIds = new List<string>();
        public Form销售记录(OrderModel orderModel, String customer, Enum订单类别 @enum)
        {
            InitializeComponent();
            this.orderModel = orderModel;
            this.customer = customer;
            this.Enum订单类别 = @enum;
            this.textBox客户.Text = this.customer;
            //筛选原来的orderDto里有用的字段转成新的dto
            this.OrderCommitDtos = new 销售记录Model().build新OrderCommitDto(this.orderModel.OrderDtos);
            //转成能提交的DTO
            this.OrderCommitFacade = new 销售记录Model().build提交List(this.OrderCommitDtos);
            //销售名细
            this.gridControl销售明细.DataSource = new 销售记录Model().build销售明细(this.OrderCommitFacade);

            this.searchLookUpEdit店铺.Properties.DataSource = 销售记录Service.get店铺();
            this.searchLookUpEdit销售员.Properties.DataSource = 销售记录Service.get销售员();
            this.searchLookUpEdit支付方式.Properties.DataSource = 销售记录Service.get支付方式();
            if (this.Enum订单类别 == Enum订单类别.Luna店铺订单)
            {
                this.shopId = "3";
                this.shopName = "Luna";
                this.empId = Frm登陆窗口.storeAssitantDTO.id;
                this.empName = Frm登陆窗口.storeAssitantDTO.name;
                this.searchLookUpEdit店铺.Text = "";
                this.searchLookUpEdit销售员.Text = "";
            }
        }
        public Form销售记录(OrderModel orderModel, String customer, Enum订单类别 @enum,List<String> quoteIds)
        {
            InitializeComponent();
            this.config读取店铺();
            this.orderModel = orderModel;
            this.customer = customer;
            this.Enum订单类别 = @enum;
            this.textBox客户.Text = this.customer;
            this.quoteIds = quoteIds;
            //筛选原来的orderDto里有用的字段转成新的dto
            //this.OrderCommitDtos = new 销售记录Model().build新OrderCommitDto(this.orderModel.OrderDtos);
            this.OrderCommitDtos = this.orderModel.orderCommitDTO返回s;
            //转成能提交的DTO
            this.OrderCommitFacade = new 销售记录Model().build提交List(this.OrderCommitDtos);
            //销售名细
            this.gridControl销售明细.DataSource = new 销售记录Model().build销售明细(this.OrderCommitFacade);

            this.searchLookUpEdit店铺.Properties.DataSource = 销售记录Service.get店铺();
            this.searchLookUpEdit销售员.Properties.DataSource = 销售记录Service.get销售员();
            this.searchLookUpEdit支付方式.Properties.DataSource = 销售记录Service.get支付方式();
        }

        private void config读取店铺()
        {
            if (System.IO.File.Exists(@"xml\config店铺.xml"))
            {
                DTO销售表头 dTO店铺 = DealXML.XMLFlieToObject<DTO销售表头>(@"xml\config店铺.xml", Encoding.UTF8);
                this.shopId = dTO店铺.id;
                this.shopName = dTO店铺.name;
                this.searchLookUpEdit店铺.Text = this.shopName;
                this.searchLookUpEdit店铺.Enabled = false;
            }
        }

        #region 选择店铺
        private void searchLookUpEdit店铺_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear店铺Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit店铺View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit店铺.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.shopId = this.searchLookUpEdit店铺.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.shopName = this.searchLookUpEdit店铺.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                    if (!System.IO.File.Exists(@"xml\config店铺.xml"))
                    {
                        DealXML.ObjectToXMLFile(new DTO销售表头(this.shopId, this.shopName), @"xml\config店铺.xml", Encoding.UTF8);
                    }
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear店铺Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit店铺.ToolTip = null;
            searchLookUpEdit店铺.EditValue = null;
        }
        private void searchLookUpEdit店铺_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.shopName;
        }
        #endregion

        #region 选择销售员
        private void searchLookUpEdit销售员_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear销售员Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit销售员View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit销售员.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.empId = this.searchLookUpEdit销售员.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.empName = this.searchLookUpEdit销售员.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear销售员Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit销售员.ToolTip = null;
            searchLookUpEdit销售员.EditValue = null;
        }
        private void searchLookUpEdit销售员_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.empName;
        }
        #endregion

        #region 选择支付方式
        private void searchLookUpEdit支付方式_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear店铺Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit支付方式View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit支付方式.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.wayId = this.searchLookUpEdit支付方式.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.wayName = this.searchLookUpEdit支付方式.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear支付方式Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit支付方式.ToolTip = null;
            searchLookUpEdit支付方式.EditValue = null;
        }
        private void searchLookUpEdit支付方式_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.wayName;
        }
        #endregion

        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            if (this.orderModel.HasOrder == true)
            {
                MessageBox.Show("请勿重复下单");
                return;
            }
            if (String.IsNullOrEmpty(this.shopId) 
                    || String.IsNullOrEmpty(this.empId)
                        || String.IsNullOrEmpty(this.wayId))
            {
                MessageBox.Show("请选择店铺/销售员/支付方式");
                return;
            }
            //门店下单及销售记录表保存
            销售记录Service.post销售记录(
                new DTO销售记录(
                    this.textBox客户.Text,
                    this.dateTimePicker时间.Value.ToString(),
                    this.shopId,
                    this.empId,
                    this.wayId,
                    this.OrderCommitFacade
                    ),
                this.Enum订单类别
                );
            //补充信息完成调用
            销售确认Service.qcItemCloseBySalComfirmIds(this.quoteIds);
            //保存备注图片
            try
            {
                foreach (OrderDto orderDto in this.orderModel.OrderDtos)
                {
                    销售记录Service.post备注图片(orderDto);
                }
            }
            catch { }
            orderModel.HasOrder = true;
            MessageBox.Show("保存成功");
            this.Close();
        }

        private void gridView销售明细_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            this.OrderCommitFacade[e.RowHandle].salesConfirmationEntryDTO.price 
                = Convert.ToDouble(e.Value);
            this.OrderCommitFacade[e.RowHandle].salesConfirmationEntryDTO.totalPrice
                = (this.OrderCommitFacade[e.RowHandle].salesConfirmationEntryDTO.price) * (this.OrderCommitFacade[e.RowHandle].salesConfirmationEntryDTO.quantity);
        }
    }
}